from datetime import datetime
from flask import session, flash, redirect, url_for, render_template, current_app
from app.main import main
from app.main.forms import UserInfoForm, NameForm, PasswordChangeForm, RegisterForm

#
# @app.before_request
# def get_initialize_count():
#     # 请求该app的次数加一
#     current_app.initialize_num['count'] += 1
#     # 如果在全局变量存的信息中get到了目前请求的url则让当前url的key值加1
#     if current_app.initialize_num["url"].get(request.url):
#         current_app.initialize_num["url"][request.url] += 1
#     else:
#         # 没有get到当前请求的url把当前url的请求次数设置为1次
#         current_app.initialize_num["url"][request.url] = 1
#     print(current_app.initialize_num)
#
# @app.before_first_request
# def initialize_count_num():
#     # 定义一个全局变量用来存放计数的日志信息
#     current_app.initialize_num = {
#         'count': 0,
#         'url':{
#
#         }
#     }
#
# # 注册一个函数，如果没有未处理的异常抛出，在每次请求之后运行
# @app.teardown_appcontext
# def get_error(e):
#     # print(g.re_url)
#     if e:
#         current_app.initialize.append({'url':g.re_url,'type': type(e), 'args': (e,)})
#     print(current_app.initialize)
#
# @app.before_request
# def get_url():
#     # 设置一个全局变量，但是每次请求页面都会刷新存储值的g
#     g.re_url = request.url
#
# @app.before_first_request
# def initialize_count():
#     # 定义一个全局变量的数组用来存放错误信息
#     current_app.initialize = []
#








@main.route("/", methods=['GET', 'POST'])
def index():
    name = session.get('name')
    form = NameForm()

    if form.validate_on_submit():
        print(form.name)
        old_name = session.get("name")
        if old_name is not None and old_name != form.name.data:
            flash("你貌似把名字改了！！！")
            name = form.name.data
            session['name'] = name
            return redirect(url_for('main.index'))
    return render_template('index.html', current_time=datetime.utcnow(), form=form, name=name)


@main.route('/user/<path:name>')
def user(name):
    print(url_for('user', name=name, _external=True))
    return render_template('index.html', name=name, current_time=datetime.utcnow(), form=NameForm())


@main.route('/pwd/', methods=['GET', 'POST'])
def PWD():
    form = PasswordChangeForm()
    if form.validate_on_submit():
        return redirect(url_for("main.index"))
    return render_template("password.html", form=form)


@main.route('/userinfo/', methods=['GET', 'POST'])
def UserInfo():
    form = UserInfoForm()
    if form.validate_on_submit():
        return redirect(url_for("main.index"))
    return render_template("password.html", form=form)


@main.before_app_first_request
def get_session():
    current_app.session_name = {}


@main.route('/register/', methods=['GET', 'POST'])
def Regieter():
    form = RegisterForm()
    if form.validate_on_submit():
        name = form.name.data
        if name in current_app.session_name:
            flash('用户名已存在!')
            return render_template('register.html', form=form)
        else:
            current_app.session_name[name] = datetime.utcnow()
            session['name'] = name
        return redirect(url_for('main.index'))
    return render_template('register.html', form=form)
